<template>
  <div class="public">
    <div class="upload-box">
      <div class="tip">游客图片不定时清理,如需长期保存请搭建私人图床, 游客最大600KB图片大小。禁止上传涉黄、涉政等违法图片</div>
      <el-upload class="upload-demo" drag action="#" :before-upload="beforeAvatarUpload" :file-list="fileList" list-type="picture" >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">
          将文件拖到此处，或
          <em>点击上传</em>
        </div>
        <div class="el-upload__tip"  slot="tip">只能上传jpg/png/gif文件，且不超过600kb</div>
      </el-upload>
    </div>
    <Drawer :urls="urls" />
  </div>
</template>

<script>
import Drawer from '@/components/Drawer'
import * as gitee from '@/api/gitee'
import * as utils from '@/utils'
export default {
  name: 'Public',
  components: {
    Drawer
  },
  data() {
    return {
      action: '',
      urls: [],
      fileList:[]
    }
  },
  mounted() {
    if (localStorage.getItem('user')) {
      this.$router.push('/')
    } else {
      this.getImgList()
    }
  
  },
  methods: {
    getImgList() {
      gitee.trees('sanghongxv', 'publicImghost').then(res => {
      this.urls = res.data.tree.map(item => {
         return {
            imgUrl: item.path.indexOf('.md')===-1?`https://gitee.com/sanghongxv/publicImghost/raw/master/${item.path}`:'',
            path: item.path
          }
      })
    })
    },
    beforeAvatarUpload(file) {
      const isJPG =
        file.type == 'image/jpeg' ||
        file.type == 'image/png' ||
        file.type == 'image/gif'
      const isLt2M = file.size / 1024 / 1024 < 0.6
      if (!isJPG) {
        this.$message.warning('上传图片只能是 JPG/PNG/GIF 格式!')
        return isJPG
      }
      if (!isLt2M) {
        this.$message.warning('上传图片大小不能超过 600kb!')
        return isLt2M
      }
      console.log(file);
      utils.getBase64(file).then(resBase64 => {
        let data = {
          access_token:'',
          content:resBase64.split(',')[1],
          message:'公共'
        }
        gitee.updata('sanghongxv', 'publicImghost',file.name,data).then(doc => {
          let date = {
            name:doc.data.content.name,
            url:doc.data.content.download_url
          }
          this.fileList.push(date)
          this.getImgList()
        })
      })
      // 
    }
  }
}
</script>
<style lang="scss" scoped>


.upload-box {
  width: 50rem;
  text-align: center;
  margin: 0 auto;
}

.tip {
  font-size: 22px;
  color: #2c3e50;
  margin: 5.75rem 0 3.5rem 0;
}

</style>
